﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Telerik.Windows.Controls;
using BrokerBase.Lib.Query;
using SaleLibrary.Sale;
using BrokerBase.Lib.Core;

namespace Broker.Mgr.SaleView
{
    /// <summary>
    /// Interaction logic for ClientListPage.xaml
    /// </summary>
    public partial class ClientListPage
    {

        private const int PAGE_SIZE = 15;

        private PagingQuery pageingQuery;

        private string hqlBase = "from Client as c ";//默认hql

        public ClientListPage()
        {
            InitializeComponent();
        }

        private void gvClient_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            View();
        }

        private void btnView_Click(object sender, RoutedEventArgs e)
        {
            View();
        }


        private void dpList_PageIndexChanged(object sender, PageIndexChangedEventArgs e)
        {
            if (pageingQuery != null)
            {
                pageingQuery.FirstResult = e.NewPageIndex * PAGE_SIZE;
                Query();
            }
        }

        private void BuildQuery()
        {
            pageingQuery = new PagingQuery();

            string where = "where 1=1";

            if (!string.IsNullOrEmpty(txtRegName.Text))
            {
                where += string.Format(" and c.Name Like '%{0}%'", txtRegName.Text.Trim());
            }

            if (dpRegStart.SelectedDate != null)
            {
                where += string.Format(" and c.RegTime>='{0}'", dpRegStart.SelectedDate.Value.ToString("yyyy-MM-dd"));
            }

            if (dpRegEnd.SelectedDate != null)
            {
                where += string.Format(" and c.RegTime<='{0}'", dpRegEnd.SelectedDate.Value.ToString("yyyy-MM-dd"));
            }

            if (!string.IsNullOrEmpty(txtPresenterName.Text))
            {
                where += string.Format(" and c.Parent.Name Like '%{0}%'", txtPresenterName.Text.Trim());
            }

            pageingQuery.HQL = hqlBase + where + "order by c.RegTime desc";
            pageingQuery.TotalHQL = "select count(*) " + hqlBase + where;
        }

        protected void Query()
        {
            try
            {
                busyRoot.IsBusy = true;
                busyRoot.BusyContent = "正在加载数据...";

                IBaseService<Client> svr = new BaseService<Client>(AppGlobal.HbmSessionFactory.GetCurrentSession());

                long total = 0;
                IList<Client> ls = svr.GetListByHQLWithPaging(pageingQuery.HQL, pageingQuery.TotalHQL, pageingQuery.FirstResult, PAGE_SIZE, out total);

                int t3 = Environment.TickCount;


                dpList.ItemCount = (int)total;
                gvClient.ItemsSource = ls;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                busyRoot.IsBusy = false;
            }

        }

        private void View()
        {
            IClient c = gvClient.SelectedItem as IClient;

            if (c != null)
            {
                ClientForm frm = new ClientForm(c);

                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.Owner = AppGlobal.MinWin;
                frm.ShowDialog();
            }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            BuildQuery();
            Query();
        }
        
    }
}
